$(() => {
    let userid = GetQueryString('userid');
    const token = GetQueryString('token');
    let page = 1;
    let pageSize = 5;
    ajaxList(page);

    // 建材圈列表
    function ajaxList(page) {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Company|single_forum', 'userid': userid, 'page': page, 'pagelimit': pageSize },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';
                    // var arr = [];
                    data.forEach(item => {
                        // // console.log(item);
                        let date = getDate(item.create_time);
                        // arr.push(item.id);
                        html += `
                        <div class="ringitem my_forum${item.id}">
                            <div class="ringtop" style="display:flex;align-items:center;">
                                <img src="${item.head_logo == '' ? '../img/orimg.png' : item.head_logo}" alt="${item.name}" class="ringtpic">
                                <div class="ringtco" style="flex:1;">
                                    <div style="display:flex;justify-content: space-between;">${item.name}<span class="ringtlr">${date}</span></div>`
                        if (item.level_logo != '') {
                            html += `<img src="${item.level_logo}" alt="${item.level_name}" class="userLevel"/>`
                        }
                        html += `</div>
                            </div>
                            <p class="ringco">${item.content}</p>
                            <p class="slh">...</p>
                            <a href="javascript:;" class="ringco2">全文</a>
                            <div class="ringlilist">`
                        item.pictures.forEach(i => {
                            html += `
                                    <img src="${i.thumb_url == '' ? 'img/banner.png' : i.thumb_url}" data-url="${i.pic_url}" alt="${i.pic_name}" class="rilipic plusBanner" />
                                `
                        })
                        html += `
                            </div>
                            <div class="browse">
                                <div class="brot">${item.read_count}人浏览<a class="del_my_forum" data-forum_id="${item.id}" href="javascript:dialog('#d1');">删除</a></div>
                                <div class="brolr">
                                    <div class="brolrlink">
                                        <span class="brolrpic brolrpic2"></span><span class="num">${item.digg_count}</span>
                                    </div>
                                </div>
                            </div>`
                        if (item.comments == '') {
                            html += '';
                        } else {
                            html += `
                            <div class="brocomment">
                                <img src="../img/brotop.png" alt="" class="brotop" />`
                            item.comments.forEach(c => {
                                html += `
                                <p class="brocoti"><a href="javascript:;" class="brocotlink">${c.nick_name}:</a>${c.content}</p>
                            `;
                            })
                            html += `</div>`
                        }
                        html += `</div>`;
                    });
                    if (data.length < pageSize) {
                        $('.notMore').css('display', 'flex');
                    }
                    $('.sh_delsit').append(html);

                    // 文本溢出显示全文按钮
                    for (var i = 0; i < $(".ringco").length; i++) {
                        let height = $(".ringco").eq(i).height(); //div的宽度
                        let scrollHeight = $(".ringco").eq(i)[0].scrollHeight; //文本的宽度
                        if (scrollHeight > height) {
                            $('.ringco2').eq(i).show();
                            $('.slh').eq(i).show();
                        } else {
                            $('.ringco2').hide();
                            $('.slh').hide();
                        }
                    }
                    // 点击全文显示
                    $('.ringco2').on('click', function() {
                        $(this).parent().find(".ringco").css('height', 'auto');
                        $(this).parent().find('.slh').hide();
                        $(this).hide();
                    })

                    // 删除
                    $('.del_my_forum').on('click', function() {
                        let forum_id = $(this).attr('data-forum_id');
                        $('.forum_id').attr('data-forum_id', forum_id);
                    })

                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    }

    // 删除建材圈
    $('#checkDel').on('click', function() {
        let forum_id = $('.forum_id').attr('data-forum_id');
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Forum|del_my_forum', 'forum_id': forum_id, 'token': token },
            dataType: 'json',
            success: function(res) {
                if (res.status == 1) {
                    $('.my_forum' + forum_id).remove();
                    alert('删除成功！！！');
                    dialogClose();
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    })

    // 下拉加载更多
    $(window).scroll(function() {
        if ($(document).scrollTop() >= $(document).height() - $(window).height() - 1) {
            // console.log('我到底了');
            if ($('.notMore').css('display') != "flex") {
                page++;
                ajaxList(page);
            }
        }
    })
})